package com.powernode.work.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.powernode.commons.utils.DateUtils;
import com.powernode.commons.utils.ResponseData;
import com.powernode.commons.utils.UUIDUtils;
import com.powernode.work.bean.TranBean;
import com.powernode.work.dao.TranMapper;
import com.powernode.work.service.TranService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class TranServiceImpl implements TranService {
    @Autowired
    private TranMapper dao;

    /**
     * 分页查询交易
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResponseData page(Integer pageNum, Integer pageSize, TranBean tran) {
        Page<Map> page = PageHelper.startPage(pageNum, pageSize);
        dao.selectAll(tran);
        return ResponseData.ok(page.getResult(),page.getTotal());
    }

    /**
     *
     * 根据主键查询交易
     * @param id
     * @return
     */
    @Override
    public ResponseData findById(String id) {
        return ResponseData.ok(dao.selectById(id));
    }

    /**
     * 添加交易
     * @param tran
     * @return
     */
    @Override
    public ResponseData add(TranBean tran) {
        //设置主键
        tran.setId(UUIDUtils.getUUID());
        //设置创建时间
        tran.setCreateTime(DateUtils.formatDateTime(new Date()));

        int row = dao.insertSelective(tran);
        if (row>0){
            return ResponseData.ok();
        }
        return ResponseData.error();
    }

    /**
     * 修改交易
     * @param tran
     * @return
     */
    @Override
    public ResponseData modify(TranBean tran) {
        //设置创建时间
        tran.setEditTime(DateUtils.formatDateTime(new Date()));

        int row = dao.updateByPrimaryKeySelective(tran);
        if (row>0){
            return ResponseData.ok();
        }
        return ResponseData.error();
    }

    /**
     * 根据主键获取原始数据
     * @param id
     * @return
     */
    public ResponseData findOriginById(String id){
        return ResponseData.ok(dao.selectByPrimaryKey(id));
    }
}
